package com.wodsy.phoenix.dataservice.settlec.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.wodsy.phoenix.datadao.settlec.dao.RelSettleOrderDao;
import com.wodsy.phoenix.datadao.settlec.dao.SettleRequestDao;
import com.wodsy.phoenix.datadao.settlec.dao.SettleReviewDao;
import com.wodsy.phoenix.dataobject.base.Pager;
import com.wodsy.phoenix.dataobject.base.PhoenixGenericResponse;
import com.wodsy.phoenix.dataobject.base.PhoenixResponseWrapper;
import com.wodsy.phoenix.dataobject.base.ResponseCodeConstants;
import com.wodsy.phoenix.dataobject.settlec.model.*;
import com.wodsy.phoenix.dataservice.settlec.service.SettleService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by Huang on 2015/4/22.
 */
@Service(interfaceName="com.wodsy.phoenix.dataservice.settlec.service.SettleService" ,timeout = 1000000)
public class SettleServiceImpl implements SettleService {

    protected Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private SettleRequestDao settleRequestDao;
    @Autowired
    private SettleReviewDao settleReviewDao;
    @Autowired
    private RelSettleOrderDao relSettleOrderDao;

    @Override
    @Transactional(value="xa",rollbackFor=Exception.class)
    public PhoenixResponseWrapper<PhoenixGenericResponse> settleRequest(Integer shopId, Integer accountId,
            List<Long> orderIds) {

        // 保存结算申请
        SettleRequest request = new SettleRequest();
        request.setShopId(shopId);
        request.setShopAccountId(accountId);
        request.setStatus(SettleRequestStatus.PROCESSING);
        request.setCreatedDate(new Date());
        request.setStatusDate(new Date());

        // TODO 根据orderId计算结算各金额
        int commissionFee = 0;
        int proceduresFee = 0;
        int realReceiveFee = 0;
        int totalFee = 0;
        for (Long orderId : orderIds) {

        }

        request.setCommissionFee(commissionFee);
        request.setProceduresFee(proceduresFee);
        request.setRealReceiveFee(realReceiveFee);
        request.setTotalFee(totalFee);

        settleRequestDao.save(request);

        Integer requestId = request.getId();

        // 保存结算审核
        SettleReview review = new SettleReview();
        review.setId(requestId);
        review.setCheckStatus(SettleReviewStatus.INIT);
        review.setCreateDate(new Date());
        settleReviewDao.save(review);

        // 保存关联关系
        for (Long orderId : orderIds) {
            RelSettleOrder settleOrder = new RelSettleOrder();
            settleOrder.setOrderId(orderId);
            settleOrder.setSettleId(requestId);
            relSettleOrderDao.save(settleOrder);
        }

        return new PhoenixResponseWrapper<PhoenixGenericResponse>(new PhoenixGenericResponse(request.getId()));
    }

    @Override
    @Transactional(value="xa",readOnly = true)
    public PhoenixResponseWrapper<SettleRequest> findSettleRequests(Pager<SettleRequest> pager) {
        try {
            Pager<SettleRequest> allSettleRequests = settleRequestDao.findSettleRequests(pager);
            return new PhoenixResponseWrapper<SettleRequest>(allSettleRequests);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new PhoenixResponseWrapper<SettleRequest>(ResponseCodeConstants.EXCEPTION,e.getMessage());
        }
    }

    @Override
    @Transactional(value="xa",readOnly = true)
    public PhoenixResponseWrapper<SettleRequest> getSettleRequest(int id) {
        try {
            SettleRequest settleRequest = settleRequestDao.get(id);
            return new PhoenixResponseWrapper<SettleRequest>(settleRequest);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new PhoenixResponseWrapper<SettleRequest>(ResponseCodeConstants.EXCEPTION,e.getMessage());
        }
    }

    @Override
    @Transactional(value="xa",readOnly = true)
    public PhoenixResponseWrapper<SettleReview> findSettleReviews(Pager<SettleReview> pager) {
        try {
            Pager<SettleReview> settleReviews = settleReviewDao.findSettleReviews(pager);
            return new PhoenixResponseWrapper<SettleReview>(settleReviews);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new PhoenixResponseWrapper<SettleReview>(ResponseCodeConstants.EXCEPTION,e.getMessage());
        }
    }

    @Override
    @Transactional(value="xa",readOnly = true)
    public PhoenixResponseWrapper<SettleReview> getSettleReview(int id) {
        SettleReview review = settleReviewDao.get(id);
        return new PhoenixResponseWrapper<SettleReview>(review);
    }

    @Override
    @Transactional(value="xa",rollbackFor=Exception.class)
    public PhoenixResponseWrapper<PhoenixGenericResponse> reviewPass(int id, int checkUserId, String remark) {
        SettleReview review = settleReviewDao.get(id);
        review.setCheckUser(checkUserId);
        review.setCheckStatus(SettleReviewStatus.PASS);
        review.setCheckDate(new Date());
        review.setRemark(remark);
        review.setUpdateTime(new Date());
        settleReviewDao.update(review);
        return new PhoenixResponseWrapper<PhoenixGenericResponse>(new PhoenixGenericResponse(id));
    }

    @Override
    @Transactional(value="xa",rollbackFor=Exception.class)
    public PhoenixResponseWrapper<PhoenixGenericResponse> reviewReject(int id, int checkUserId, String remark) {
        SettleReview review = settleReviewDao.get(id);
        review.setCheckUser(checkUserId);
        review.setCheckStatus(SettleReviewStatus.REJECT);
        review.setCheckDate(new Date());
        review.setRemark(remark);
        review.setUpdateTime(new Date());
        settleReviewDao.update(review);
        // 同时更新结算失败
        SettleRequest request = settleRequestDao.get(id);
        request.setStatus(SettleRequestStatus.FAILED);
        request.setStatusDate(new Date());
        settleRequestDao.update(request);
        return new PhoenixResponseWrapper<PhoenixGenericResponse>(new PhoenixGenericResponse(id));
    }

    @Override
    @Transactional(value="xa",rollbackFor=Exception.class)
    public PhoenixResponseWrapper<PhoenixGenericResponse> settleComfirm(int id, int settleMoney, String sellerAccount,
            String settleWay, int comfirmUserId) {
        SettleReview review = settleReviewDao.get(id);
        review.setCheckStatus(SettleReviewStatus.COMFIRM);
        review.setSettleMoney(settleMoney);
        review.setSellerAccount(sellerAccount);
        review.setSettleWay(settleWay);
        review.setConfirmUser(comfirmUserId);
        review.setConfirmDate(new Date());
        review.setUpdateTime(new Date());
        // 同时更新结算单完成
        SettleRequest request = settleRequestDao.get(id);
        request.setStatus(SettleRequestStatus.COMPLETED);
        request.setStatusDate(new Date());
        settleRequestDao.update(request);
        return new PhoenixResponseWrapper<PhoenixGenericResponse>(new PhoenixGenericResponse(id));
    }

    public static void main(String[] args) {
        com.alibaba.dubbo.container.Main.main(args);
    }
}
